package cn.com.inossem.operation;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import cn.com.inossem.config.WSConfig;
import cn.com.inossem.model.LoginresultData;
import cn.com.inossem.network.NetworkConnect;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.Utils;

import com.foxykeep.datadroid.exception.ConnectionException;
import com.foxykeep.datadroid.exception.CustomRequestException;
import com.foxykeep.datadroid.exception.DataException;
import com.foxykeep.datadroid.requestmanager.Request;
import com.foxykeep.datadroid.service.RequestService.Operation;
import com.google.gson.Gson;

public class RegisteredOperation implements Operation {
	public static Bundle bundle;

	public static final String REGISTERED_OPERATION_USERNAME = "registered_operation_username";
	public static final String REGISTERED_OPERATION_PASSWORD = "registered_operation_password";
	public static final String REGISTERED_OPERATION_VALIDCODE = "registered_operation_validcode";
	public static final String REGISTERED_OPERATION_TRUENAME = "registered_operation_truename";
	public static final String REGISTERED_OPERATION_EMAIL = "registered_operation_email";
	public static final String REGISTERED_OPERATION_INVITEUSER = "registered_operation_inviteuser";

	@Override
	public Bundle execute(Context arg0, Request request)
			throws ConnectionException, DataException, CustomRequestException {

		String uName = request.getString(REGISTERED_OPERATION_USERNAME);
		String uPWD = request.getString(REGISTERED_OPERATION_PASSWORD);
		String validcode = request.getString(REGISTERED_OPERATION_VALIDCODE);
		String truename = request.getString(REGISTERED_OPERATION_TRUENAME);
		String email = request.getString(REGISTERED_OPERATION_EMAIL);
		String inviteuser = request.getString(REGISTERED_OPERATION_INVITEUSER);

		NetworkConnect connection = new NetworkConnect(WSConfig.LOCAL_URL
				+ "userInfo/addUser");
		connection.setPostText("{\"username\":\"" + uName
				+ "\",\"password\":\"" + uPWD 
				+ "\",\"truename\":\"" + truename
				+ "\",\"email\":\"" + email
				+ "\",\"inviteuser\":\"" + inviteuser 
				+ "\",\"validcode\":\"" + validcode + "\"}");
		System.out.println("{\"username\":\"" + uName + "\",\"password\":\""
				+ uPWD + "\",\"truename\":\"" + truename + "\",\"email\":\""
				+ email + "\",\"validcode\":\"" + validcode + "\"}");
		String body = connection.execute();

		Log.i("RegisteredOperation:", body);
		bundle = new Bundle();
		try {
			JSONObject jsonObject = new JSONObject(body);
			int errorFlag = jsonObject.getInt("errorFlag");
			// List<SkillsInfoBean> retList;
			if (errorFlag == WSConfig.RESULT_SUCESS) {

				Gson datagson = new Gson();
				Utils.messageInfo = datagson.fromJson(body,
						LoginresultData.class);
				Utils.isDenglu = 1;

			} else {
				// retList = null;
			}
			// 返回返回值
			// bundle.putParcelableArrayList(
			// WooRequestFactory.RESPONSE_SKILLS_DATA,
			// (ArrayList<? extends Parcelable>) retList);
			// // 设置结果
			bundle.putInt(WooRequestFactory.BUNDLE_EXTRA_REGISTERED, errorFlag);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return bundle;
	}
}
